﻿using BenchmarkDotNet.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BenchMarkSamples.BenchCase.SyncAndAsync
{
    /// <summary>
    /// 同步异步方法的性能测试
    /// </summary>
    [SimpleJob(launchCount: 2, warmupCount: 2, iterationCount: 3)]
    [RankColumn]
    public class SyncAndAsyncDemo
    {
        [Benchmark]
        public int CounterNumber()
        {
            int counter = 0;
            for (int i = 0; i < 5_000_000; i++)
            {
                counter += i;
            }
            return counter;
        }

        [Benchmark]
        public async Task<int> AsyncCounterNumber()
        {
            var result = await Task.Run(() => {
                int counter = 0;
                for (int i = 0; i < 5_000_000; i++) {
                    counter += i;
                }
                return counter;
            });

            return result;
        }
    }
}
